package com.augrain.microframework.core.spring;

import org.springframework.boot.context.event.ApplicationPreparedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.MapPropertySource;

/**
 * 初始化SpringUtils 之所以放到listener中，是方便在beanRegist时就能拿到SpringUtils
 *
 * @author biaoy
 * @since 2020/11/25
 */
@Order(Integer.MIN_VALUE)
public class SpringUtilPreInitializer implements ApplicationListener<ApplicationPreparedEvent> {

    @Override
    public void onApplicationEvent(ApplicationPreparedEvent applicationEvent) {
        ConfigurableApplicationContext applicationContext = applicationEvent.getApplicationContext();
        SpringUtils.setApplicationContext(applicationContext);
        MapPropertySource mapPropertySource = new MapPropertySource("customPropertySource", SpringUtils.customProperty);
        applicationContext.getEnvironment().getPropertySources().addLast(mapPropertySource);
    }
}
